var express = require('express');
var router = express.Router();
const userModel = require('./../model/usersModel')

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});
router.post('/register', function(req, res, next) {
  let {username,password,age} = req.body
  userModel.create({
    username,password,age
  }).then((data)=>{
    res.status(200)
    res.send({
      data:{
        code:0,
        message:'注册成功'
      }
    });
  }).catch(e=>{
    res.status(200)
    res.send({
      data:{
        code:-1,
        message:'注册失败'
      }
    });
  })
});

router.post('/get_user_list',function (req,res,next){
  userModel.find().then((data)=>{
    res.status(200)
    res.send({
      data:{
        code:0,
        data:data,
      }
    });
  }).catch(e=>{
    res.status(200)
    res.send({
      data:{
        code:0,
        message:'获取数据失败',
      }
    });
  })
})

router.post('/del_user',function (req,res,next){
  let {id} = req.body
  console.log(req.body)
  console.log(id)
  userModel.deleteOne({_id:id}).then((data)=>{
    console.log('data---删除----',data)
    res.status(200)
    res.send({
      data:{
        code:0,
        message:'删除成功',
      }
    });
  }).catch(e=>{
    res.status(200)
    res.send({
      data:{
        code:0,
        message:'获取数据失败',
      }
    });
  })
})

router.post('/edit_user',function (req,res,next){
  let {id} = req.body
  userModel.updateOne({_id:id},{age:10}).then((data)=>{
    res.status(200)
    res.send({
      data:{
        code:0,
        message:'修改成功',
      }
    });
  }).catch(e=>{
    res.status(200)
    res.send({
      data:{
        code:0,
        message:'修改数据失败',
      }
    });
  })
})

module.exports = router;
